package com.imm.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.imm.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;


/**
 * 元数据对象处理器
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Resource
    private HttpServletRequest request;

    @Override
    public void insertFill(MetaObject metaObject) {
        Long id = BaseContext.get();
        LocalDateTime now = LocalDateTime.now();
        metaObject.setValue("createTime", now);
        if (metaObject.hasSetter("updateTime")) {
            metaObject.setValue("updateTime", now);
        }
        if (metaObject.hasSetter("createUser"))
            metaObject.setValue("createUser", id);
        if (metaObject.hasSetter("updateUser"))
            metaObject.setValue("updateUser", id);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Long id = BaseContext.get();
        LocalDateTime now = LocalDateTime.now();
        metaObject.setValue("updateTime", now);
        metaObject.setValue("updateUser", id);
    }
}
